package com.xiaolin.sports.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xiaolin.sports.common.enums.PayType;
import com.xiaolin.sports.common.enums.SignResource;
import com.xiaolin.sports.common.enums.TradeType;
import com.xiaolin.sports.dao.ChildSignInfoMapper;
import com.xiaolin.sports.dao.CourseInfoMapper;
import com.xiaolin.sports.dao.ParentInfoMapper;
import com.xiaolin.sports.dao.ProductOrderMapper;
import com.xiaolin.sports.model.ParentInfo;
import com.xiaolin.sports.model.ProductOrder;
import com.xiaolin.sports.page.Page;
import com.xiaolin.sports.reqVo.CourseSignVO;
import com.xiaolin.sports.service.IProductOrderService;
import com.xiaolin.sports.utils.StringUtil;
import com.xiaolin.sports.view.CourseListView;
import com.xiaolin.sports.view.CourseSignOrderView;

@Service("productOrderService")
public class ProductOrderServiceImpl implements IProductOrderService {

	@Autowired
	private ProductOrderMapper productOrderMapper;
	@Autowired
	private CourseInfoMapper courseInfoMapper;
	@Autowired
	private ChildSignInfoMapper childSignInfoMapper;
	@Autowired
	private ParentInfoMapper parentInfoMapper;
	
	@Override
	public ProductOrder selectByOrderNo(String orderNO) {
		return productOrderMapper.selectByOrderNo(orderNO);
	}

	@Override
	public ProductOrder selectByParam(String openid, String orderNo) {
		return productOrderMapper.selectByParam(openid, orderNo);
	}

	@Override
	public int updateById(ProductOrder vo) {
		return productOrderMapper.updateByPrimaryKeySelective(vo);
	}

	@Override
	public List<CourseSignOrderView> selectCourseSignOrderByPage(Page<CourseSignOrderView> page) {
		return productOrderMapper.selectCourseSignOrderByPage(page);
	}

	@Override
	public CourseSignOrderView selectCourseSignOrderByOrderId(Integer orderId) {
		return productOrderMapper.selectCourseSignOrderByOrderId(orderId);
	}

	@Override
	public ProductOrder selectById(Integer id) {
		return productOrderMapper.selectByPrimaryKey(id);
	}

	@Override
	public int updateInvalidOrder() {
		return productOrderMapper.updateInvalidOrder();
	}

	@Override
	public ProductOrder addUPdateOrder(CourseSignVO info) {
		CourseListView courseInfo = courseInfoMapper.selectForWapByParams(info.getOpenid(), info.getProductId());
		ParentInfo parentInfo = parentInfoMapper.selectByOpenid(info.getOpenid());
		if(parentInfo == null) {
			parentInfo = new ParentInfo();
			parentInfo.setOpenid(info.getOpenid());
			parentInfo.setParentMobile(info.getEmerMobile());
			parentInfo.setParentName(info.getEmerName());
			parentInfo.setCity(info.getCity());
			parentInfo.setHeadImg(info.getHeadImg());
			parentInfo.setNickName(info.getNickName());
			parentInfoMapper.insertSelective(parentInfo);
		}
		ProductOrder productOrder = new ProductOrder();
		productOrder.setSignName(info.getEmerName());
		productOrder.setSignPhone(info.getEmerMobile());
		productOrder.setOpenid(info.getOpenid());
		productOrder.setOrderNo(StringUtil.getCourseOrderNum(parentInfo.getId()));
		productOrder.setTotalAmount(courseInfo.getCurrentPrice());
		productOrder.setCustId(parentInfo.getId());
		productOrder.setProductId(courseInfo.getId());
		productOrder.setChannel(info.getChannel());
		productOrder.setPaySubmitTime(new Date());
		if(StringUtils.isNotBlank(info.getChannel()) && info.getChannel().equals(PayType.WEIXIN.getCode())) {
			productOrder.setTradeType(TradeType.JSAPI.getCode());
		}
		productOrderMapper.insertSelective(productOrder);
		info.setParentId(parentInfo.getId());
		info.setSignResource(parentInfo.getSchoolId() == null ? SignResource.ONLINE.getCode() : SignResource.SCHOOL.getCode());
		info.setProductId(courseInfo.getId());
		info.setOrderNo(productOrder.getOrderNo());
		childSignInfoMapper.insertSelective(info);
		productOrder.setProductName(courseInfo.getCourseName());
		return productOrder;
	}

}
